package me.android.library.common.service;

import android.content.Context;

import java.lang.ref.WeakReference;

import lombok.extern.slf4j.Slf4j;
import me.android.library.utils.android.ContextUtil;
import mylab.base.foundation.event.AsyncEventSupporter;

@Slf4j
public abstract class AbstractService implements AsyncEventSupporter {

    private WeakReference<Context> contextRef;

    public AbstractService() {
    }

    public void init() {
        log.info("service init:{}", getClass().getName());
        contextRef = new WeakReference<>(ContextUtil.getAppContext());
        AsyncEventSupporter.super.registerEventListener();
    }

    public void dispose() {
        log.info("service dispose:{}", getClass().getName());
        AsyncEventSupporter.super.unregisterEventListener();
        contextRef.clear();
    }

    public Context getContext() {
        return contextRef != null ? contextRef.get() : null;
    }

}
